<?php
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Regex as RegexValidator;
use Phalcon\Validation\Validator\Between;
use Phalcon\Validation\Validator\StringLength;

class MenuController extends ControllerBase
{

    public function addAction(){

    }

    public function doAddAction(){
        $menuName=$this->getPost("menu_name");
        $menuUrl=$this->getPost("menu_url");
        $preMenuId=$this->getPost("pre_menu_id");
        $menuOrder=$this->getPost("menu_order");

        header('Content-type: application/json');
        header('Access-Control-Allow-Origin:*');
        if(empty($menuName)){
            $this->returnError("菜单名称为空");
        }
        if(empty($preMenuId)){
            $preMenuId=0;
        }else{
            $preMenuId=intval($preMenuId);
        }
        if(empty($menuOrder)){
            $menuOrder=0;
        }else{
            $menuOrder=intval($menuOrder);
        }

        $resultId = $this->insert("system_menu", array(
            "menu_name" => $menuName,
            "menu_url" => $menuUrl,
            "pre_menu_id" => $preMenuId,
            "menu_order" => $menuOrder,
        ));

        if($resultId>0){
            $this->returnSuccess("添加成功");
        }else{
            $this->returnError("添加失败");
        }
    }

    public function doListAction(){
        $data=$this->fetchAll("select * from system_menu ");
        header('Content-type: application/json');
        header('Access-Control-Allow-Origin:*');
        $this->returnJson($data);
    }

    public function doDeleteAction()
    {
        header('Content-type: application/json');
        header('Access-Control-Allow-Origin:*');
        $menuId=$this->getPost("menu_id");
        if(empty($menuId)) {
            $this->returnError("删除失败");
        }else{
            $resultId = $this->execute("delete from system_menu where id=$menuId");
            if($resultId>0){
                $this->returnSuccess("删除成功");
            }else{
                $this->returnError("删除失败");
            }
        }
    }


}